//
//  UpInMusicViewController.m
//  K歌之王
//
//  Created by 小晓 on 16/1/3.
//  Copyright © 2016年 xiao. All rights reserved.
//

#import "UpInMusicViewController.h"
#import "KMusicTableViewCell.h"
#import "KMusicMsgDataModel.h"
#import "MusicClassTableViewController.h"
#import "MusicAllAndAloneViewController.h"

#define mainscreenwidht [UIScreen mainScreen].bounds.size.width
#define mainscreenheight [UIScreen mainScreen].bounds.size.height

@interface UpInMusicViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    UITableView * haveSomeTabelView;
}

@property (strong,nonatomic)NSMutableArray * haveSomeArray;

@end

@implementation UpInMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"点歌台";
    [self initView];
}

-(void)viewWillAppear:(BOOL)animated{
     self.tabBarController.tabBar.hidden = YES;
    [super viewWillAppear:animated];

    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(backBtnAction)];
    [self.navigationController.navigationBar setHidden:NO];
}

-(void)backBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
    //  [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

- (void)initView{
    UIView * upInView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainscreenwidht, 105)];
    upInView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:upInView];
    
    UIControl * singerControl = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, upInView.bounds.size.width/2, 75)];
    singerControl.tag = 10;
    [singerControl addTarget:self action:@selector(singerUpInCtr:) forControlEvents:(UIControlEventTouchUpInside)];
    [upInView addSubview:singerControl];
    
    UIImageView * singerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, singerControl.bounds.size.width-10, 70)];
    singerImageView.image = [UIImage imageNamed:@"组-1@2x.png"];
    [singerControl addSubview:singerImageView];
    
    UILabel * singerLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 30, singerImageView.bounds.size.width, 20)];
    singerLabel.text = @"歌星点歌";
    singerLabel.textAlignment = NSTextAlignmentCenter;
    singerLabel.textColor = [UIColor whiteColor];
    [singerControl addSubview:singerLabel];
    
    UIControl * classControl = [[UIControl alloc]initWithFrame:CGRectMake(upInView.bounds.size.width/2, 0, upInView.bounds.size.width/2, 70)];
    classControl.tag = 11;
    [classControl addTarget:self action:@selector(singerUpInCtr:) forControlEvents:(UIControlEventTouchUpInside)];
    [upInView addSubview:classControl];
    
    UIImageView * classImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, classControl.bounds.size.width-10, 70)];
    classImageView.image = [UIImage imageNamed:@"组-1@3x_54.png"];
    [classControl addSubview:classImageView];
    
    UILabel * classLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 30, classImageView.bounds.size.width, 20)];
    classLabel.text = @"分类点歌";
    classLabel.textAlignment = NSTextAlignmentCenter;
    classLabel.textColor = [UIColor whiteColor];
    [classControl addSubview:classLabel];
    
    UIImageView * musicImageView =[[UIImageView alloc]initWithFrame:CGRectMake(15, 83, 14, 14)];
    musicImageView.image = [UIImage imageNamed:@"图层-31@2x.png"];
    [upInView addSubview:musicImageView];
    
    UILabel * musicLabel = [[UILabel alloc]initWithFrame:CGRectMake(32, 84, 100, 12)];
    musicLabel.text = @"已点歌曲";
    musicLabel.font = [UIFont systemFontOfSize:16];
    musicLabel.textColor = [UIColor orangeColor];
    [upInView addSubview:musicLabel];
    
    _haveSomeArray = [[NSMutableArray alloc]init];
    
    haveSomeTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 105, mainscreenwidht, mainscreenheight-169) style:(UITableViewStylePlain)];
    haveSomeTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    haveSomeTabelView.allowsSelection = YES;
    haveSomeTabelView.delegate = self;
    haveSomeTabelView.dataSource = self;
    [self.view addSubview:haveSomeTabelView];
    
    [self requestData];
}

-(void)requestData{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString *userId = [FileStorage NSUserDefault_getObjectForKey:@"User_id"];
    NSString*infoStr11=[NSString stringWithFormat:@"/api/records/%@/all",userId];
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //        [_messageArray removeAllObjects];
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        NSDictionary * bodyDict = [dic objectForKey:@"body"];
        NSArray * contentArray = [bodyDict objectForKey:@"content"];
        for(NSDictionary * contentDict in contentArray){
            HotMusicMsgDataModel *upinMusic = [[HotMusicMsgDataModel alloc]init];
            upinMusic.avatar = [contentDict objectForKey:@"avatar"];
            upinMusic.name = [contentDict objectForKey:@"name"];
            NSString * str = [NSString stringWithFormat:@"%@",[contentDict objectForKey:@"size"]];
            float s = [str floatValue];
            s = s/1024/100;
            upinMusic.size = [NSString stringWithFormat:@"%d",(int)s];
            upinMusic.singerName = [contentDict objectForKey:@"songerName"];
            upinMusic.accompanyUrl = [contentDict objectForKey:@"accompanyUrl"];
            upinMusic.lyric = [contentDict objectForKey:@"lyric"];
            [upinMusic fixedString];
            [_haveSomeArray addObject:upinMusic];
        }
        [haveSomeTabelView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.haveSomeArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KMusicTableViewCell"];
    if ( !cell ) {
        cell = [[KMusicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"KMusicTableViewCell"];
    }
    HotMusicMsgDataModel *upinMusic = [self.haveSomeArray objectAtIndex:indexPath.row];
    [(KMusicTableViewCell *)cell updataCellViewWithHeadData:upinMusic superViewCtr:self];
    
    return cell;
}

#pragma mark - Table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 71;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MusicAllAndAloneViewController *allAndAloneViewCtr = [MusicAllAndAloneViewController new];
    
    [self.navigationController pushViewController:allAndAloneViewCtr animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) singerUpInCtr:(UIControl *)sender{
    MusicClassTableViewController *classMiscTabelView = [[MusicClassTableViewController alloc]init];
    if(sender.tag == 10){
        classMiscTabelView.titleName = @"歌星点歌";
    }else if(sender.tag ==11){
        classMiscTabelView.titleName = @"分类点歌";
    }
    [self.navigationController pushViewController:classMiscTabelView animated:YES];
}

@end
